/**
 * 
 */
package com.cipe.cmrs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.model.Cipeprojecttype;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.util.DateUtil;
import com.cipe.cmrs.util.Util;

/**
 * @author Incepio
 * 
 */
@Service
@Transactional
public class CipeprojecttypeService extends BaseService<Cipeprojecttype> {

	@Autowired
	private UserService userService;

	public Cipeprojecttype saveCipeprojecttype(Cipeprojecttype cipeprojecttype, Integer userId) {
		if (cipeprojecttype != null) {
			User user = userService.findById(userId);
			Cipeprojecttype dbCipeprojecttype = findById(cipeprojecttype.getId());
			if (dbCipeprojecttype == null) {
				dbCipeprojecttype = new Cipeprojecttype();
				dbCipeprojecttype.setCreatedBy(user);
				dbCipeprojecttype.setCreationDate(DateUtil.getCurrentDate());
			}
			dbCipeprojecttype.setDisplayName(cipeprojecttype.getDisplayName());
			dbCipeprojecttype.setDescription(cipeprojecttype.getDescription());
			dbCipeprojecttype.setUpdatedBy(user);
			dbCipeprojecttype.setUpdationDate(DateUtil.getCurrentDate());
			return merge(dbCipeprojecttype);
		}
		return null;
	}

	/**
	 * @param displayName
	 * @return
	 */
	public Cipeprojecttype findByDisplayName(String displayName) {
		Cipeprojecttype cipeprojecttype = null;
		try {
			String query = "from Cipeprojecttype where 1=1 " + Util.createQueryClause("displayName", displayName);
			cipeprojecttype = this.dao.getSingleResult(query);
		} catch (Exception exp) {
			logger.error(exp.getMessage(), exp);
		}
		return cipeprojecttype;
	}
	
	public List<Cipeprojecttype> getAllProjectTypesForProject() {
		StringBuilder queryBuilder = new StringBuilder();
		queryBuilder.append("SELECT obj FROM Cipeprojecttype obj WHERE ");
		queryBuilder.append("obj.id != " + getText("id.projecttype.management"));
		queryBuilder.append(" AND ");
		queryBuilder.append("obj.id != " + getText("id.projecttype.nonpartnered"));
		queryBuilder.append(" AND ");
		queryBuilder.append("obj.id != " + getText("id.projecttype.partner"));
		queryBuilder.append(" AND ");
		queryBuilder.append("obj.id != " + getText("id.projecttype.consultant"));
		queryBuilder.append(" AND ");
		queryBuilder.append("obj.id != " + getText("id.projecttype.administrationindirect"));
		return dao.getResultList(queryBuilder.toString());
	}
}
